package com.zhengke.controller.hubf;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.javassist.expr.NewArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhengke.pojo.AjaxResult;
import com.zhengke.pojo.ReservationLimitEntity;
import com.zhengke.pojo.SysOrderResult;
import com.zhengke.pojo.SysareaEntity;
import com.zhengke.pojo.SysareaEntityTree;
import com.zhengke.service.ReservationLimitManagementService;


@Controller
@RequestMapping("/reservation")
public class ReservationLimitManagementController {
	
	@Autowired
	ReservationLimitManagementService reservationService;
	
	@RequestMapping("/gotoJsp.do")
	public String gotoTreeMethod(ModelMap model) {
		model.put("tmp", new Date().getTime());
		return "reservation/reservationTree";
	}
	/**
	 * 跳转到展示的界面的方法
	 * @param id
	 * @return
	 */
	@RequestMapping("/gotoReservationUpdJsp.do")
	public String gotoReservationUpdMethod(String areaId,ModelMap model) {
		model.put("area_id", areaId);
		model.put("tmp", new Date().getTime());
		
		return "reservation/reservationUpd";
	}
	
	@RequestMapping("/getTree.do")
	@ResponseBody
	public List<SysareaEntityTree> querryEntityTreesMethod(String id){
		if(id == null ) {
			List<SysareaEntity> sysareaEntityList = reservationService.querryReservationLimitManagementTreeMethod("0");//第一次登录查询所有的最父级的所有的根节点
			//创建集合装进去
			ArrayList<SysareaEntityTree> treeList = new ArrayList<SysareaEntityTree>();
			//遍历集合将内容传输过去
			for(SysareaEntity sysera : sysareaEntityList) {
				SysareaEntityTree sysareaTree = new SysareaEntityTree();
				sysareaTree.setId(sysera.getArea_id());
				sysareaTree.setName(sysera.getArea_name());
				sysareaTree.setDescription(sysera.getArea_descr());
				sysareaTree.setIsParent(sysera.getCount() > 0);
				treeList.add(sysareaTree);
			}
			return treeList;
		}else {
			List<SysareaEntity> sysareaEntityList = reservationService.querryReservationLimitManagementTreeMethod(id);//第一次登录查询所有的最父级的所有的根节点
			//创建集合装进去
			ArrayList<SysareaEntityTree> treeList = new ArrayList<SysareaEntityTree>();
			//遍历集合将内容传输过去
			for(SysareaEntity sysera : sysareaEntityList) {
				SysareaEntityTree sysareaTree = new SysareaEntityTree();
				sysareaTree.setName(sysera.getArea_name());
				sysareaTree.setId(sysera.getArea_id());
				sysareaTree.setDescription(sysera.getArea_descr());
				sysareaTree.setIsParent(sysera.getCount() > 0);
				treeList.add(sysareaTree);
			}
			return treeList;
		}
	}
	
	@RequestMapping("/getTable.do")
	@ResponseBody
	public SysOrderResult getTableMethod( String area_id,int page,int limit) {
		System.out.println("*********************************++++++++++*******************************"+area_id);
		String countString = reservationService.getTotalMethod(area_id);
		long cout = Long.parseLong(countString);
		PageHelper.startPage(page,limit,true);
		ArrayList<ReservationLimitEntity> reservList = new ArrayList<ReservationLimitEntity>();
		try {
		Map<String,Object> messaMap = new HashMap<String, Object>();
		messaMap.put("area_id",area_id);
		//根据地区的id进行查询
		List<Map<String, Object>> messageList = reservationService.querryReservationGoodsByAreaId(messaMap);
		//遍历添加数据
		for(Map<String, Object> map : messageList) {
			ReservationLimitEntity reserEntity = new ReservationLimitEntity();
			reserEntity.setGoods_name(map.get("goods_name")+"");
			reserEntity.setGoods_id(map.get("goods_id")+"");
			
			String priceString = map.get("goods_price")+"";
			double price = new Double(priceString);
		    DecimalFormat df = new DecimalFormat( "0.00");
		    String newPriceString = df.format(price);
		    double newPrice = new Double(newPriceString);
			reserEntity.setCommon_price(newPrice);
			
			reserEntity.setSpecifications(map.get("specifications")+"");
			reserEntity.setGoods_type(map.get("type")+"");
			reserEntity.setGoods_destine_quota(new Integer(map.get("goods_destine_quota")+""));
			
			
			String oldsorrage = map.get("goods_storage")+"";
			double newsorragee = new Double(oldsorrage);
			String newsorrage =  df.format(newsorragee);
			double newsorrag = new Double(newsorrage);
			reserEntity.setGoods_storage(newsorrag);
			
			
			String oldShiString = map.get("shipping_fee")+"";
			double oldShippiingFree = new Double(oldShiString);
			String newShiString = df.format(oldShippiingFree);
			double newShippingpriuce = new Double(newShiString);
			reserEntity.setShipping_fee(newShippingpriuce);
			
			String nomal = map.get("is_normal")+"";
			reserEntity.setNomal(nomal);
			
			reservList.add(reserEntity);
			}
			}catch(Exception e) {
				e.getStackTrace();
			}
		SysOrderResult sysOrderResult = new SysOrderResult();
		sysOrderResult.setCode("0");
		sysOrderResult.setData(reservList);
		sysOrderResult.setCount(cout);
		return sysOrderResult;
		
	}
	
	@RequestMapping("/deleteMethod.do")
	@ResponseBody
	public AjaxResult deleteReservationMethod(String id) {
		int result = reservationService.deleteGoodsById(id);
		if(result > 0) {
			return new AjaxResult("200","删除成功！！");
		}else {
			return new AjaxResult("300","删除失败！！");
		}
	}
	
	/**
	   * 跳转到添加的界面
	 */
	@RequestMapping("/toAddUser.do")
	public String addGoodsMethod() {
		return "reservation/reservationadd";
	}
	
	/**
	 * 跳转到修改的界面
	 */
	@RequestMapping("/toChec.do")
	public String modifyMethod() {
		
		return "reservation/checkPage";
	}
	
	@RequestMapping("/updateGoods.do")
	@ResponseBody
	public AjaxResult updateGoodsLimit(HttpServletRequest request,HttpServletResponse response) {
		String goodsIdString = request.getParameter("goodsId");
		String desTineString = request.getParameter("goods_destine_quota");
		Map<String,Object> messageMap = new HashMap<String, Object>();
		messageMap.put("goods_destine_quota", desTineString);
		messageMap.put("goods_id", goodsIdString);
		int restul = reservationService.updateGoodLImit(messageMap);
		if(restul > 0) {
			return new AjaxResult("200","··修改成功··");
		}else {
			return new AjaxResult("300","**修改失败**");
		}
	}
}
